package com.cndw.kungfu.comm;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsDayIntKey;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.Aes;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.ext.WordProcesser;
import com.cndw.rpg.framework.Session;

public class AccessInspection {
	private static Set<String> lockIpSet = new HashSet<String>();

	/**
	 * 重置
	 */
	public synchronized static void reloadLockIp() {

		lockIpSet.clear();
		List<String> ips = ConstantsConfig.getSysLockIp();
		for (String ip : ips) {
			ip = ip.replace(".*", "");
			lockIpSet.add(ip);
		}

	}

	/**
	 * 是否gm帐号
	 * 
	 * @param accountName
	 * @return
	 */
	public static boolean isGmAccountName(String accountName) {
		return accountName != null && ConstantsConfig.getSysGmAccountName().contains(accountName);
	}

	public static boolean isChatLockAcc(String accountName) {
		return accountName != null && ConstantsConfig.getSysLockChatAcc().contains(accountName);
	}

	private static boolean isChatLockIp(String ip) {
		return ip != null && ConstantsConfig.getSysLockChatIP().contains(ip);
	}

	public static boolean isChatLockPlayer(Player player) {
		return isChatLockAcc(player.getAccountName()) || isChatLockIp(player.getLastIp());
	}

	public static boolean isForbidIpEntirely(String sourceIp) {
		return sourceIp != null && ConstantsConfig.getSysLockIp().contains(sourceIp);
	}

	/**
	 * 是否禁用ip
	 * 
	 * @param sourceIp
	 * @return
	 */
	public static boolean isForbidIp(String sourceIp) {
		return isForbidIpEntirely(sourceIp);
		/*
		 * if (sourceIp == null) { return false; } String[] ipWold =
		 * sourceIp.split("\\."); StringBuilder sb = new StringBuilder(); for
		 * (int i = 0; i < ipWold.length; i++) { sb.append(ipWold[i]); if
		 * (lockIpSet.contains(sb.toString())) { return true; } sb.append(".");
		 * }
		 * 
		 * return false;
		 */
	}

	@Deprecated
	public static boolean checkIp(String sourceIp) {
		List<String> ips = ConstantsConfig.getSysLockIp();// 需要过滤的ip
		for (String ip : ips) {
			if (matchIp(ip, sourceIp)) {
				return true;
			}
		}

		return false;
	}

	@Deprecated
	public static boolean matchIp(String ip, String sourceIp) {
		try {
			if (ip.contains(ConstantsModel.wildcard)) {
				int index = ip.indexOf(ConstantsModel.wildcard.charAt(0));
				if (ip.regionMatches(0, sourceIp, 0, index)) {
					String[] ipWord = ip.substring(index, ip.length()).split("\\.");
					String[] surceIpWord = sourceIp.substring(index, sourceIp.length()).split("\\.");
					for (int i = 0; i < surceIpWord.length; i++) {
						if (!ipWord[i].equals(ConstantsModel.wildcard) && !ipWord[i].equals(surceIpWord[i])) {
							return false;
						}
					}
					return true;
				} else {
					return false;
				}
			} else {
				return ip.equals(sourceIp);
			}
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 封号检查
	 * 
	 * @return
	 */
	public static boolean checkAccount(long playerId) {
		return playerId > 0 && check(ConstantsConfig.getSysLockPlayerId(), playerId);
	}

	/**
	 * 禁言检查
	 * 
	 * @return
	 */
	public static boolean checkChat(long playerId) {
		return check(ConstantsConfig.getSysLockChat(), playerId);
	}

	public static int chatLockTime(long playerId) {
		if (ConstantsConfig.getSysLockChat().containsKey(playerId)) {
			return ConstantsConfig.getSysLockChat().get(playerId);
		}
		return 0;
	}

	public static boolean addChatLock(long playerId, int addTime) {
		Map<Long, Integer> lockPlayers = ConstantsConfig.getSysLockChat();
		int lastTime = 0;
		if (lockPlayers.containsKey(playerId)) {
			lastTime = lockPlayers.get(playerId);
		}
		lastTime = lastTime < TimeProcesser.getUnixTime() ? TimeProcesser.getUnixTime() : lastTime;
		lockPlayers.put(playerId, lastTime + addTime);
		return true;
	}

	private static boolean check(Map<Long, Integer> players, long playerId) {
		return players.containsKey(playerId) && players.get(playerId) > TimeProcesser.getUnixTime();
	}

	/**
	 * 对称加密检查
	 * 
	 * @param cipherText
	 * @param params
	 * @return
	 */
	public static boolean validateRequest(String cipherText, String... params) {
		if (cipherText == null || cipherText.length() == 0) {
			return false;
		}
		String aes = Aes.decrypt(cipherText);
		if (aes == null) {
			return false;
		}
		String[] param = aes.split("\r\n");

		if ((param.length - 1) != params.length) {
			return false;
		}

		for (int i = 0, j = params.length; i < j; i++) {
			if (!param[i].equals(params[i])) {
				return false;
			}
		}

		int time = Integer.parseInt(param[param.length - 1]);
		if (!ConstantsConfig.isLocalHost() && TimeProcesser.getUnixTime() > (time + 180)) {
			return false;
		}

		return true;
	}

	/**
	 * 是否内部帐号
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isGmAcc(Session session) {
		if (session == null | session.getAccountName() == null) {
			return false;
		}
		return isGmAccountName(session.getAccountName());
	}

	public static boolean isGmIp(Session session) {
		return ConstantsConfig.getSysGmIp() != null && ConstantsConfig.getSysGmIp().contains(session.getRemoteAddress());
	}

	public static boolean isCrossIp(Session session) {
		return ConstantsConfig.getSysCgCrossIP() != null && ConstantsConfig.getSysCgCrossIP().contains(session.getRemoteAddress());
	}
	
	public static boolean isGmInstructor(String acc){
		if (acc == null || ConstantsConfig.getSysGmInstructor() == null) {
			return false;
		}
		return ConstantsConfig.getSysGmInstructor().contains(acc);
	}

	public static boolean passOnColsed(Session session) {
		return isGmAcc(session) || isCrossIp(session) || isGmIp(session);
	}

	public static boolean isForbidChat(Player player, String msg) {
		if ((player.getLevel() < 42 && player.getDataInfo().intDataGet(ConstantsIntKey.payCash) == 0 && (isChatLockIp(player.getLastIp()) || WordProcesser
						.isAd(player.getPlayerName().concat(msg))))) {

			player.getDataInfo().getEveryDay().intDataAlter(ConstantsDayIntKey.chatAd, 1);
			int chatAdNums = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.chatAd);
			if (player.getLevel() <= 2 && chatAdNums > 2) {
				chatAdNums += 100;
			}
			AccessInspection.addChatLock(player.getPlayerId(), 30 + 300 * chatAdNums);
			return true;
		} else {
			return false;
		}

	}
}
